package com.yint.tree;

import java.util.List;

/**
 * 选择复合节点
 * <p>
 * 直到有一个子节点tick成功或子节点全部失败
 * <p>
 * Created by 殷田 on 2018/5/3.
 */
public class Selector extends Composite {

    @Override
    protected synchronized STATUS onTick(Tree tree) {
        List<Leaf> subs = getSubs();
        if (subs != null && subs.size() > 0) {
            for (Leaf sub : subs) {
                if (sub.entry(tree)) {
                    STATUS state = sub.tick(tree);
                    if (state != STATUS.FAILED) {
                        return state;
                    }
                } else {
                    sub.leave(tree);
                }
            }
        }
        return STATUS.FAILED;
    }
}
